<!DOCTYPE HTML>
<html lang="en">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="AverageJoe&#39;s Blog">
    <meta name="description" content="this is my secret garden where I sow my inspirations.">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>AverageJoe&#39;s Blog</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery.min.js"></script>
<meta name="generator" content="Hexo 5.0.0"><link rel="alternate" href="/atom.xml" title="AverageJoe's Blog" type="application/atom+xml">
</head>


<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">AverageJoe&#39;s Blog</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>Index</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>Tags</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>Categories</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>Archives</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>About</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>Contact</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>Friends</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="Search" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">AverageJoe&#39;s Blog</div>
        <div class="logo-desc">
            
            this is my secret garden where I sow my inspirations.
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			Index
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			Tags
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			Categories
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			Archives
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			About
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			Contact
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			Friends
		</a>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    
<script src="/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('请输入访问本文章的密码')).toString(CryptoJS.enc.Hex)) {
                alert('密码错误，将返回主页！');
                location.href = '/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/0.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title"></h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        margin: 35px 0 15px 0;
        padding-left: 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        height: calc(100vh - 250px);
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                          <div class="article-tag">
                            <span class="chip bg-color">No tag</span>
                          </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>Publish Date:&nbsp;&nbsp;
                    2018-10-14
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>Update Date:&nbsp;&nbsp;
                    2018-10-14
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>Word Count:&nbsp;&nbsp;
                    7.9k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>Read Times:&nbsp;&nbsp;
                    35 Min
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>Read Count:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">
        <div class="card-content article-card-content">
            <div id="articleContent">
                <h1 id="0-学习目标"><a href="#0-学习目标" class="headerlink" title="0.学习目标"></a>0.学习目标</h1><ul>
<li>独立编写数据导入功能</li>
<li>独立实现基本搜索</li>
<li>独立实现页面分页</li>
<li>独立实现结果排序</li>
</ul>
<h1 id="1-索引库数据导入"><a href="#1-索引库数据导入" class="headerlink" title="1.索引库数据导入"></a>1.索引库数据导入</h1><p>昨天我们学习了Elasticsearch的基本应用。今天就学以致用，搭建搜索微服务，实现搜索功能。</p>
<h2 id="1-1-创建搜索服务"><a href="#1-1-创建搜索服务" class="headerlink" title="1.1.创建搜索服务"></a>1.1.创建搜索服务</h2><p>创建module：</p>
<p><img src="assets/1532178218793.png" alt="1532178218793"></p>
<p><img src="assets/1532178276070.png" alt="1532178276070"></p>
<p>Pom文件：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">project</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://maven.apache.org/POM/4.0.0&quot;</span></span></span><br><span class="line"><span class="tag">         <span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span></span><br><span class="line"><span class="tag">         <span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">parent</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>leyou<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.leyou.parent<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.0.0-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">parent</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">modelVersion</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">modelVersion</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.leyou.search<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>leyou-search<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.0.0-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- web --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- elasticsearch --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-data-elasticsearch<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- eureka --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-netflix-eureka-client<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- feign --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-openfeign<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">project</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>application.yml：</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server:</span></span><br><span class="line">  <span class="attr">port:</span> <span class="number">8083</span></span><br><span class="line"><span class="attr">spring:</span></span><br><span class="line">  <span class="attr">application:</span></span><br><span class="line">    <span class="attr">name:</span> <span class="string">search-service</span></span><br><span class="line">  <span class="attr">data:</span></span><br><span class="line">    <span class="attr">elasticsearch:</span></span><br><span class="line">      <span class="attr">cluster-name:</span> <span class="string">elasticsearch</span></span><br><span class="line">      <span class="attr">cluster-nodes:</span> <span class="number">192.168</span><span class="number">.56</span><span class="number">.101</span><span class="string">:9300</span></span><br><span class="line"><span class="attr">eureka:</span></span><br><span class="line">  <span class="attr">client:</span></span><br><span class="line">    <span class="attr">service-url:</span></span><br><span class="line">      <span class="attr">defaultZone:</span> <span class="string">http://127.0.0.1:10086/eureka</span></span><br><span class="line">  <span class="attr">instance:</span></span><br><span class="line">    <span class="attr">lease-renewal-interval-in-seconds:</span> <span class="number">5</span> <span class="comment"># 每隔5秒发送一次心跳</span></span><br><span class="line">    <span class="attr">lease-expiration-duration-in-seconds:</span> <span class="number">10</span> <span class="comment"># 10秒不发送就过期</span></span><br><span class="line">    <span class="attr">prefer-ip-address:</span> <span class="literal">true</span></span><br><span class="line">    <span class="attr">ip-address:</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span></span><br><span class="line">    <span class="attr">instance-id:</span> <span class="string">$&#123;spring.application.name&#125;:$&#123;server.port&#125;</span></span><br></pre></td></tr></table></figure>

<p>启动类：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="meta">@EnableDiscoveryClient</span></span><br><span class="line"><span class="meta">@EnableFeignClients</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LySearchService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        SpringApplication.run(LySearchService.class, args);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="1-2-索引库数据格式分析"><a href="#1-2-索引库数据格式分析" class="headerlink" title="1.2.索引库数据格式分析"></a>1.2.索引库数据格式分析</h2><p>接下来，我们需要商品数据导入索引库，便于用户搜索。</p>
<p>那么问题来了，我们有SPU和SKU，到底如何保存到索引库？</p>
<h3 id="1-2-1-以结果为导向"><a href="#1-2-1-以结果为导向" class="headerlink" title="1.2.1.以结果为导向"></a>1.2.1.以结果为导向</h3><p>大家来看下搜索结果页：</p>
<p><img src="assets/1532180648745.png" alt="1532180648745"></p>
<p>可以看到，每一个搜索结果都有至少1个商品，当我们选择大图下方的小图，商品会跟着变化。</p>
<p>因此，<strong>搜索的结果是SPU，即多个SKU的集合</strong>。</p>
<p>既然搜索的结果是SPU，那么我们索引库中存储的应该也是SPU，但是却需要包含SKU的信息。</p>
<h3 id="1-2-2-需要什么数据"><a href="#1-2-2-需要什么数据" class="headerlink" title="1.2.2.需要什么数据"></a>1.2.2.需要什么数据</h3><p>再来看看页面中有什么数据：</p>
<p> <img src="assets/1526607712207.png" alt="1526607712207"> </p>
<p>直观能看到的：图片、价格、标题、副标题</p>
<p>暗藏的数据：spu的id，sku的id</p>
<p>另外，页面还有过滤条件：</p>
<p> <img src="assets/1526608095471.png" alt="1526608095471"></p>
<p>这些过滤条件也都需要存储到索引库中，包括：</p>
<p>商品分类、品牌、可用来搜索的规格参数等</p>
<p>综上所述，我们需要的数据格式有：</p>
<p>spuId、SkuId、商品分类id、品牌id、图片、价格、商品的创建时间、sku信息集、可搜索的规格参数</p>
<h3 id="1-2-3-最终的数据结构"><a href="#1-2-3-最终的数据结构" class="headerlink" title="1.2.3.最终的数据结构"></a>1.2.3.最终的数据结构</h3><p>我们创建一个类，封装要保存到索引库的数据，并设置映射属性：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Document(indexName = &quot;goods&quot;, type = &quot;docs&quot;, shards = 1, replicas = 0)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Goods</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Id</span></span><br><span class="line">    <span class="keyword">private</span> Long id; <span class="comment">// spuId</span></span><br><span class="line">    <span class="meta">@Field(type = FieldType.Text, analyzer = &quot;ik_max_word&quot;)</span></span><br><span class="line">    <span class="keyword">private</span> String all; <span class="comment">// 所有需要被搜索的信息，包含标题，分类，甚至品牌</span></span><br><span class="line">    <span class="meta">@Field(type = FieldType.Keyword, index = false)</span></span><br><span class="line">    <span class="keyword">private</span> String subTitle;<span class="comment">// 卖点</span></span><br><span class="line">    <span class="keyword">private</span> Long brandId;<span class="comment">// 品牌id</span></span><br><span class="line">    <span class="keyword">private</span> Long cid1;<span class="comment">// 1级分类id</span></span><br><span class="line">    <span class="keyword">private</span> Long cid2;<span class="comment">// 2级分类id</span></span><br><span class="line">    <span class="keyword">private</span> Long cid3;<span class="comment">// 3级分类id</span></span><br><span class="line">    <span class="keyword">private</span> Date createTime;<span class="comment">// 创建时间</span></span><br><span class="line">    <span class="keyword">private</span> List&lt;Long&gt; price;<span class="comment">// 价格</span></span><br><span class="line">    <span class="meta">@Field(type = FieldType.Keyword, index = false)</span></span><br><span class="line">    <span class="keyword">private</span> String skus;<span class="comment">// sku信息的json结构</span></span><br><span class="line">    <span class="keyword">private</span> Map&lt;String, Object&gt; specs;<span class="comment">// 可搜索的规格参数，key是参数名，值是参数值</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>一些特殊字段解释：</p>
<ul>
<li><p>all：用来进行全文检索的字段，里面包含标题、商品分类信息</p>
</li>
<li><p>price：价格数组，是所有sku的价格集合。方便根据价格进行筛选过滤</p>
</li>
<li><p>skus：用于页面展示的sku信息，不索引，不搜索。包含skuId、image、price、title字段</p>
</li>
<li><p>specs：所有规格参数的集合。key是参数名，值是参数值。</p>
<p>例如：我们在specs中存储 内存：4G,6G，颜色为红色，转为json就是：</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="attr">&quot;specs&quot;</span>:&#123;</span><br><span class="line">        <span class="attr">&quot;内存&quot;</span>:[<span class="number">4</span>G,<span class="number">6</span>G],</span><br><span class="line">        <span class="attr">&quot;颜色&quot;</span>:<span class="string">&quot;红色&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>当存储到索引库时，elasticsearch会处理为两个字段：</p>
<ul>
<li>specs.内存：[4G,6G]</li>
<li>specs.颜色：红色</li>
</ul>
<p>另外， 对于字符串类型，还会额外存储一个字段，这个字段不会分词，用作聚合。</p>
<ul>
<li>specs.颜色.keyword：红色</li>
</ul>
</li>
</ul>
<h2 id="1-3-商品微服务提供接口"><a href="#1-3-商品微服务提供接口" class="headerlink" title="1.3.商品微服务提供接口"></a>1.3.商品微服务提供接口</h2><p>索引库中的数据来自于数据库，我们不能直接去查询商品的数据库，因为真实开发中，每个微服务都是相互独立的，包括数据库也是一样。所以我们只能调用商品微服务提供的接口服务。</p>
<p>先思考我们需要的数据：</p>
<ul>
<li><p>SPU信息</p>
</li>
<li><p>SKU信息</p>
</li>
<li><p>SPU的详情</p>
</li>
<li><p>商品分类名称（拼接all字段）</p>
</li>
</ul>
<p>再思考我们需要哪些服务：</p>
<ul>
<li>第一：分批查询spu的服务，已经写过。</li>
<li>第二：根据spuId查询sku的服务，已经写过</li>
<li>第三：根据spuId查询SpuDetail的服务，已经写过</li>
<li>第四：根据商品分类id，查询商品分类名称，没写过</li>
<li>第五：根据商品品牌id，查询商品的品牌，没写过</li>
</ul>
<p>因此我们需要额外提供一个查询商品分类名称的接口。</p>
<h3 id="1-3-1-商品分类名称查询"><a href="#1-3-1-商品分类名称查询" class="headerlink" title="1.3.1.商品分类名称查询"></a>1.3.1.商品分类名称查询</h3><p>controller：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 根据商品分类id查询名称</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> ids 要查询的分类id集合</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 多个名称的集合</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@GetMapping(&quot;names&quot;)</span></span><br><span class="line"><span class="keyword">public</span> ResponseEntity&lt;List&lt;String&gt;&gt; queryNameByIds(<span class="meta">@RequestParam(&quot;ids&quot;)</span> List&lt;Long&gt; ids)&#123;</span><br><span class="line">    List&lt;String &gt; list = <span class="keyword">this</span>.categoryService.queryNameByIds(ids);</span><br><span class="line">    <span class="keyword">if</span> (list == <span class="keyword">null</span> || list.size() &lt; <span class="number">1</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> ResponseEntity&lt;&gt;(HttpStatus.NOT_FOUND);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> ResponseEntity.ok(list);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>测试：</p>
<p><img src="assets/1532213731039.png" alt="1532213731039"></p>
<h3 id="1-3-2-编写FeignClient"><a href="#1-3-2-编写FeignClient" class="headerlink" title="1.3.2.编写FeignClient"></a>1.3.2.编写FeignClient</h3><h4 id="1-3-2-1-问题展现"><a href="#1-3-2-1-问题展现" class="headerlink" title="1.3.2.1.问题展现"></a>1.3.2.1.问题展现</h4><p>操作leyou-search工程</p>
<p>现在，我们要在搜索微服务调用商品微服务的接口。</p>
<p>第一步要引入商品微服务依赖：<code>leyou-item-interface</code>。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--商品微服务--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.leyou.service<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>ly-item-interface<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>$&#123;leyou.latest.version&#125;<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>第二步，编写FeignClient</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@FeignClient(value = &quot;item-service&quot;)</span></span><br><span class="line"><span class="meta">@RequestMapping(&quot;/goods&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">GoodsClient</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 分页查询商品</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> page</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> rows</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> saleable</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> key</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@GetMapping(&quot;/spu/page&quot;)</span></span><br><span class="line">    ResponseEntity&lt;PageResult&lt;SpuBo&gt;&gt; querySpuByPage(</span><br><span class="line">            <span class="meta">@RequestParam(value = &quot;page&quot;, defaultValue = &quot;1&quot;)</span> Integer page,</span><br><span class="line">            <span class="meta">@RequestParam(value = &quot;rows&quot;, defaultValue = &quot;5&quot;)</span> Integer rows,</span><br><span class="line">            <span class="meta">@RequestParam(value = &quot;saleable&quot;, defaultValue = &quot;true&quot;)</span> Boolean saleable,</span><br><span class="line">            <span class="meta">@RequestParam(value = &quot;key&quot;, required = false)</span> String key);</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 根据spu商品id查询详情</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> id</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@GetMapping(&quot;/spu/detail/&#123;id&#125;&quot;)</span></span><br><span class="line">    <span class="function">ResponseEntity&lt;SpuDetail&gt; <span class="title">querySpuDetailById</span><span class="params">(<span class="meta">@PathVariable(&quot;id&quot;)</span> Long id)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 根据spu的id查询sku</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> id</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@GetMapping(&quot;sku/list&quot;)</span></span><br><span class="line">    ResponseEntity&lt;List&lt;Sku&gt;&gt; querySkuBySpuId(<span class="meta">@RequestParam(&quot;id&quot;)</span> Long id);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>以上的这些代码直接从商品微服务中拷贝而来，完全一致。差别就是没有方法的具体实现。大家觉得这样有没有问题？</p>
<p>而FeignClient代码遵循SpringMVC的风格，因此与商品微服务的Controller完全一致。这样就存在一定的问题：</p>
<ul>
<li>代码冗余。尽管不用写实现，只是写接口，但服务调用方要写与服务controller一致的代码，有几个消费者就要写几次。</li>
<li>增加开发成本。调用方还得清楚知道接口的路径，才能编写正确的FeignClient。</li>
</ul>
<h4 id="1-3-2-2-解决方案"><a href="#1-3-2-2-解决方案" class="headerlink" title="1.3.2.2.解决方案"></a>1.3.2.2.解决方案</h4><p>因此，一种比较友好的实践是这样的：</p>
<ul>
<li>我们的服务提供方不仅提供实体类，还要提供api接口声明</li>
<li>调用方不用字自己编写接口方法声明，直接继承提供方给的Api接口即可，</li>
</ul>
<p>第一步：服务的提供方在<code>leyou-item-interface</code>中提供API接口，并编写接口声明：</p>
<p> <img src="assets/1526613268722.png" alt="1526613268722"></p>
<p>商品分类服务接口：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;category&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">CategoryApi</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@GetMapping(&quot;names&quot;)</span></span><br><span class="line">    ResponseEntity&lt;List&lt;String&gt;&gt; queryNameByIds(<span class="meta">@RequestParam(&quot;ids&quot;)</span> List&lt;Long&gt; ids);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>商品服务接口，返回值不再使用ResponseEntity：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(&quot;/goods&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">GoodsApi</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 分页查询商品</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> page</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> rows</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> saleable</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> key</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@GetMapping(&quot;/spu/page&quot;)</span></span><br><span class="line">    <span class="function">PageResult&lt;SpuBo&gt; <span class="title">querySpuByPage</span><span class="params">(</span></span></span><br><span class="line"><span class="function"><span class="params">            <span class="meta">@RequestParam(value = &quot;page&quot;, defaultValue = &quot;1&quot;)</span> Integer page,</span></span></span><br><span class="line"><span class="function"><span class="params">            <span class="meta">@RequestParam(value = &quot;rows&quot;, defaultValue = &quot;5&quot;)</span> Integer rows,</span></span></span><br><span class="line"><span class="function"><span class="params">            <span class="meta">@RequestParam(value = &quot;saleable&quot;, defaultValue = &quot;true&quot;)</span> Boolean saleable,</span></span></span><br><span class="line"><span class="function"><span class="params">            <span class="meta">@RequestParam(value = &quot;key&quot;, required = false)</span> String key)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 根据spu商品id查询详情</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> id</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@GetMapping(&quot;/spu/detail/&#123;id&#125;&quot;)</span></span><br><span class="line">    <span class="function">SpuDetail <span class="title">querySpuDetailById</span><span class="params">(<span class="meta">@PathVariable(&quot;id&quot;)</span> Long id)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 根据spu的id查询sku</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> id</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@GetMapping(&quot;sku/list&quot;)</span></span><br><span class="line">    <span class="function">List&lt;Sku&gt; <span class="title">querySkuBySpuId</span><span class="params">(<span class="meta">@RequestParam(&quot;id&quot;)</span> Long id)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>需要引入springMVC及leyou-common的依赖：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-webmvc<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>5.0.6.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.leyou.common<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>leyou-common<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.0.0-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>



<p>第二步：在调用方<code>leyou-search</code>中编写FeignClient，但不要写方法声明了，直接继承<code>leyou-item-interface</code>提供的api接口：</p>
<p>商品的FeignClient：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@FeignClient(value = &quot;item-service&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">GoodsClient</span> <span class="keyword">extends</span> <span class="title">GoodsApi</span> </span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>商品分类的FeignClient：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@FeignClient(value = &quot;item-service&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">CategoryClient</span> <span class="keyword">extends</span> <span class="title">CategoryApi</span> </span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>是不是简单多了？</p>
<p>项目结构：</p>
<p> <img src="assets/1532215914558.png" alt="1532215914558"></p>
<p> <img src="assets/1532218231760.png" alt="1532218231760"></p>
<h4 id="1-3-2-3-测试"><a href="#1-3-2-3-测试" class="headerlink" title="1.3.2.3.测试"></a>1.3.2.3.测试</h4><p>在leyou-search中引入springtest依赖：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-test<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>创建测试类：</p>
<p>在接口上按快捷键：<code>Ctrl + Shift + T</code></p>
<p> <img src="assets/1532216103709.png" alt="1532216103709"></p>
<p> <img src="assets/1532216169168.png" alt="1532216169168"></p>
<p>测试代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RunWith(SpringRunner.class)</span></span><br><span class="line"><span class="meta">@SpringBootTest(classes = LeyouSearchApplication.class)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CategoryClientTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> CategoryClient categoryClient;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testQueryCategories</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        List&lt;String&gt; names = <span class="keyword">this</span>.categoryClient.queryNameByIds(Arrays.asList(<span class="number">1L</span>, <span class="number">2L</span>, <span class="number">3L</span>));</span><br><span class="line">        names.forEach(System.out::println);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>结果：</p>
<p><img src="assets/1532216884221.png" alt="1532216884221"></p>
<h2 id="1-4-导入数据"><a href="#1-4-导入数据" class="headerlink" title="1.4.导入数据"></a>1.4.导入数据</h2><p>导入数据只做一次,以后的更新删除等操作通过消息队列来操作索引库</p>
<h3 id="1-4-1-创建GoodsRepository"><a href="#1-4-1-创建GoodsRepository" class="headerlink" title="1.4.1.创建GoodsRepository"></a>1.4.1.创建GoodsRepository</h3><p>java代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">GoodsRepository</span> <span class="keyword">extends</span> <span class="title">ElasticsearchRepository</span>&lt;<span class="title">Goods</span>, <span class="title">Long</span>&gt; </span>&#123;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="1-4-2-创建索引"><a href="#1-4-2-创建索引" class="headerlink" title="1.4.2.创建索引"></a>1.4.2.创建索引</h3><p>我们新建一个测试类，在里面进行数据的操作：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RunWith(SpringRunner.class)</span></span><br><span class="line"><span class="meta">@SpringBootTest(classes = LeyouSearchApplication.class)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ElasticsearchTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> GoodsRepository goodsRepository;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> ElasticsearchTemplate elasticsearchTemplate;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">createIndex</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">// 创建索引</span></span><br><span class="line">        <span class="keyword">this</span>.elasticsearchTemplate.createIndex(Goods.class);</span><br><span class="line">        <span class="comment">// 配置映射</span></span><br><span class="line">        <span class="keyword">this</span>.elasticsearchTemplate.putMapping(Goods.class);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>通过kibana查看：</p>
<p><img src="assets/1532217819818.png" alt="1532217819818"></p>
<h3 id="1-4-3-导入数据"><a href="#1-4-3-导入数据" class="headerlink" title="1.4.3.导入数据"></a>1.4.3.导入数据</h3><p>导入数据其实就是查询数据，然后把查询到的Spu转变为Goods来保存，因此我们先编写一个SearchService，然后在里面定义一个方法， 把Spu转为Goods</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SearchService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> CategoryClient categoryClient;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> GoodsClient goodsClient;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> SpecificationClient specificationClient;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> ObjectMapper mapper = <span class="keyword">new</span> ObjectMapper();</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Goods <span class="title">buildGoods</span><span class="params">(Spu spu)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">        Goods goods = <span class="keyword">new</span> Goods();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 查询商品分类名称</span></span><br><span class="line">        List&lt;String&gt; names = <span class="keyword">this</span>.categoryClient.queryNameByIds(Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()));</span><br><span class="line">        <span class="comment">// 查询sku</span></span><br><span class="line">        List&lt;Sku&gt; skus = <span class="keyword">this</span>.goodsClient.querySkuBySpuId(spu.getId());</span><br><span class="line">        <span class="comment">// 查询详情</span></span><br><span class="line">        SpuDetail spuDetail = <span class="keyword">this</span>.goodsClient.querySpuDetailById(spu.getId());</span><br><span class="line">        <span class="comment">// 查询规格参数</span></span><br><span class="line">        List&lt;SpecParam&gt; params = <span class="keyword">this</span>.specificationClient.querySpecParam(<span class="keyword">null</span>, spu.getCid3(), <span class="keyword">true</span>, <span class="keyword">null</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 处理sku，仅封装id、价格、标题、图片，并获得价格集合</span></span><br><span class="line">        List&lt;Long&gt; prices = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">        List&lt;Map&lt;String, Object&gt;&gt; skuList = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">        skus.forEach(sku -&gt; &#123;</span><br><span class="line">            prices.add(sku.getPrice());</span><br><span class="line">            Map&lt;String, Object&gt; skuMap = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">            skuMap.put(<span class="string">&quot;id&quot;</span>, sku.getId());</span><br><span class="line">            skuMap.put(<span class="string">&quot;title&quot;</span>, sku.getTitle());</span><br><span class="line">            skuMap.put(<span class="string">&quot;price&quot;</span>, sku.getPrice());</span><br><span class="line">            skuMap.put(<span class="string">&quot;image&quot;</span>, StringUtils.isBlank(sku.getImages()) ? <span class="string">&quot;&quot;</span> : StringUtils.split(sku.getImages(), <span class="string">&quot;,&quot;</span>)[<span class="number">0</span>]);</span><br><span class="line">            skuList.add(skuMap);</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 处理规格参数</span></span><br><span class="line">        Map&lt;String, Object&gt; genericSpecs = mapper.readValue(spuDetail.getGenericSpec(), <span class="keyword">new</span> TypeReference&lt;Map&lt;String, Object&gt;&gt;() &#123;</span><br><span class="line">        &#125;);</span><br><span class="line">        Map&lt;String, Object&gt; specialSpecs = mapper.readValue(spuDetail.getSpecialSpec(), <span class="keyword">new</span> TypeReference&lt;Map&lt;String, Object&gt;&gt;() &#123;</span><br><span class="line">        &#125;);</span><br><span class="line">        <span class="comment">// 获取可搜索的规格参数</span></span><br><span class="line">        Map&lt;String, Object&gt; searchSpec = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 过滤规格模板，把所有可搜索的信息保存到Map中</span></span><br><span class="line">        Map&lt;String, Object&gt; specMap = <span class="keyword">new</span> HashMap&lt;&gt;();</span><br><span class="line">        params.forEach(p -&gt; &#123;</span><br><span class="line">            <span class="keyword">if</span> (p.getSearching()) &#123;</span><br><span class="line">                <span class="keyword">if</span> (p.getGeneric()) &#123;</span><br><span class="line">                    String value = genericSpecs.get(p.getId().toString()).toString();</span><br><span class="line">                    <span class="keyword">if</span>(p.getNumeric())&#123;</span><br><span class="line">                        value = chooseSegment(value, p);</span><br><span class="line">                    &#125;</span><br><span class="line">                    specMap.put(p.getName(), StringUtils.isBlank(value) ? <span class="string">&quot;其它&quot;</span> : value);</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    specMap.put(p.getName(), specialSpecs.get(p.getId().toString()));</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        goods.setId(spu.getId());</span><br><span class="line">        goods.setSubTitle(spu.getSubTitle());</span><br><span class="line">        goods.setBrandId(spu.getBrandId());</span><br><span class="line">        goods.setCid1(spu.getCid1());</span><br><span class="line">        goods.setCid2(spu.getCid2());</span><br><span class="line">        goods.setCid3(spu.getCid3());</span><br><span class="line">        goods.setCreateTime(spu.getCreateTime());</span><br><span class="line">        goods.setAll(spu.getTitle() + <span class="string">&quot; &quot;</span> + StringUtils.join(names, <span class="string">&quot; &quot;</span>));</span><br><span class="line">        goods.setPrice(prices);</span><br><span class="line">        goods.setSkus(mapper.writeValueAsString(skuList));</span><br><span class="line">        goods.setSpecs(specMap);</span><br><span class="line">        <span class="keyword">return</span> goods;</span><br><span class="line">    &#125;                                                   </span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>因为过滤参数中有一类比较特殊，就是数值区间：</p>
<p><img src="assets/1526608095471.png" alt="1526608095471"></p>
<p>所以我们在存入时要进行处理：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">private</span> String <span class="title">chooseSegment</span><span class="params">(String value, SpecParam p)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">double</span> val = NumberUtils.toDouble(value);</span><br><span class="line">    String result = <span class="string">&quot;其它&quot;</span>;</span><br><span class="line">    <span class="comment">// 保存数值段</span></span><br><span class="line">    <span class="keyword">for</span> (String segment : p.getSegments().split(<span class="string">&quot;,&quot;</span>)) &#123;</span><br><span class="line">        String[] segs = segment.split(<span class="string">&quot;-&quot;</span>);</span><br><span class="line">        <span class="comment">// 获取数值范围</span></span><br><span class="line">        <span class="keyword">double</span> begin = NumberUtils.toDouble(segs[<span class="number">0</span>]);</span><br><span class="line">        <span class="keyword">double</span> end = Double.MAX_VALUE;</span><br><span class="line">        <span class="keyword">if</span>(segs.length == <span class="number">2</span>)&#123;</span><br><span class="line">            end = NumberUtils.toDouble(segs[<span class="number">1</span>]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 判断是否在范围内</span></span><br><span class="line">        <span class="keyword">if</span>(val &gt;= begin &amp;&amp; val &lt; end)&#123;</span><br><span class="line">            <span class="keyword">if</span>(segs.length == <span class="number">1</span>)&#123;</span><br><span class="line">                result = segs[<span class="number">0</span>] + p.getUnit() + <span class="string">&quot;以上&quot;</span>;</span><br><span class="line">            &#125;<span class="keyword">else</span> <span class="keyword">if</span>(begin == <span class="number">0</span>)&#123;</span><br><span class="line">                result = segs[<span class="number">1</span>] + p.getUnit() + <span class="string">&quot;以下&quot;</span>;</span><br><span class="line">            &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                result = segment + p.getUnit();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>然后编写一个测试类，循环查询Spu，然后调用IndexService中的方法，把SPU变为Goods，然后写入索引库：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">loadData</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="comment">// 创建索引</span></span><br><span class="line">    <span class="keyword">this</span>.elasticsearchTemplate.createIndex(Goods.class);</span><br><span class="line">    <span class="comment">// 配置映射</span></span><br><span class="line">    <span class="keyword">this</span>.elasticsearchTemplate.putMapping(Goods.class);</span><br><span class="line">    <span class="keyword">int</span> page = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">int</span> rows = <span class="number">100</span>;</span><br><span class="line">    <span class="keyword">int</span> size = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">do</span> &#123;</span><br><span class="line">        <span class="comment">// 查询分页数据</span></span><br><span class="line">        PageResult&lt;SpuBo&gt; result = <span class="keyword">this</span>.goodsClient.querySpuByPage(page, rows, <span class="keyword">true</span>, <span class="keyword">null</span>);</span><br><span class="line">        List&lt;SpuBo&gt; spus = result.getItems();</span><br><span class="line">        size = spus.size();</span><br><span class="line">        <span class="comment">// 创建Goods集合</span></span><br><span class="line">        List&lt;Goods&gt; goodsList = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">        <span class="comment">// 遍历spu</span></span><br><span class="line">        <span class="keyword">for</span> (SpuBo spu : spus) &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                Goods goods = <span class="keyword">this</span>.searchService.buildGoods(spu);</span><br><span class="line">                goodsList.add(goods);</span><br><span class="line">            &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">this</span>.goodsRepository.saveAll(goodsList);</span><br><span class="line">        page++;</span><br><span class="line">    &#125; <span class="keyword">while</span> (size == <span class="number">100</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>通过kibana查询， 可以看到数据成功导入：</p>
<p><img src="assets/1532228358310.png" alt="1532228358310"></p>
<h1 id="2-实现基本搜索"><a href="#2-实现基本搜索" class="headerlink" title="2.实现基本搜索"></a>2.实现基本搜索</h1><h2 id="2-1-页面分析"><a href="#2-1-页面分析" class="headerlink" title="2.1.页面分析"></a>2.1.页面分析</h2><h3 id="2-1-1-页面跳转"><a href="#2-1-1-页面跳转" class="headerlink" title="2.1.1.页面跳转"></a>2.1.1.页面跳转</h3><p>在首页的顶部，有一个输入框：</p>
<p><img src="assets/1526629923970.png" alt="1526629923970"></p>
<p>当我们输入任何文本，点击搜索，就会跳转到搜索页<code>search.html</code>了：</p>
<p>并且将搜索关键字以请求参数携带过来：</p>
<p><img src="assets/1532229236516.png" alt="1532229236516"></p>
<p>我们打开<code>search.html</code>，在最下面会有提前定义好的Vue实例：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span>&gt;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> vm = <span class="keyword">new</span> Vue(&#123;</span></span><br><span class="line"><span class="javascript">        el: <span class="string">&quot;#searchApp&quot;</span>,</span></span><br><span class="line">        data: &#123;</span><br><span class="line">        &#125;,</span><br><span class="line">        components:&#123;</span><br><span class="line"><span class="javascript">            <span class="comment">// 加载页面顶部组件</span></span></span><br><span class="line"><span class="javascript">            lyTop: <span class="function">() =&gt;</span> <span class="keyword">import</span>(<span class="string">&quot;./js/pages/top.js&quot;</span>)</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>这个Vue实例中，通过import导入的方式，加载了另外一个js：top.js并作为一个局部组件。top其实是页面顶部导航组件，我们暂时不管</p>
<h3 id="2-1-2-发起异步请求"><a href="#2-1-2-发起异步请求" class="headerlink" title="2.1.2.发起异步请求"></a>2.1.2.发起异步请求</h3><p>要想在页面加载后，就展示出搜索结果。我们应该在页面加载时，获取地址栏请求参数，并发起异步请求，查询后台数据，然后在页面渲染。</p>
<p>我们在data中定义一个对象，记录请求的参数：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">data: &#123;</span><br><span class="line">    search:&#123;</span><br><span class="line">        key:<span class="string">&quot;&quot;</span>, <span class="comment">// 搜索页面的关键字</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>我们通过钩子函数created，在页面加载时获取请求参数，并记录下来。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">created()&#123;</span><br><span class="line">    <span class="comment">// 判断是否有请求参数</span></span><br><span class="line">    <span class="keyword">if</span>(!location.search)&#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 将请求参数转为对象</span></span><br><span class="line">    <span class="keyword">const</span> search = ly.parse(location.search.substring(<span class="number">1</span>));</span><br><span class="line">    <span class="comment">// 记录在data的search对象中</span></span><br><span class="line">    <span class="built_in">this</span>.search = search;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 发起请求，根据条件搜索</span></span><br><span class="line">    <span class="built_in">this</span>.loadData();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后发起请求，搜索数据。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">methods: &#123;</span><br><span class="line">    loadData()&#123;</span><br><span class="line">        <span class="comment">// ly.http.post(&quot;/search/page&quot;, ly.stringify(this.search)).then(resp=&gt;&#123;</span></span><br><span class="line">        ly.http.post(<span class="string">&quot;/search/page&quot;</span>, <span class="built_in">this</span>.search).then(<span class="function"><span class="params">resp</span>=&gt;</span>&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(resp);</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>我们这里使用<code>ly</code>是common.js中定义的工具对象。</li>
<li>这里使用的是post请求，这样可以携带更多参数，并且以json格式发送</li>
</ul>
<p>在leyou-gateway中，添加允许信任域名：</p>
<p><img src="assets/1532233280898.png" alt="1532233280898"></p>
<p>并添加网关映射：</p>
<p><img src="assets/1532233247824.png" alt="1532233247824"></p>
<p>刷新页面试试：</p>
<p><img src="assets/1532233086523.png" alt="1532233086523"></p>
<p>因为后台没有提供接口，所以无法访问。没关系，接下来我们实现后台接口</p>
<h2 id="2-2-后台提供搜索接口"><a href="#2-2-后台提供搜索接口" class="headerlink" title="2.2.后台提供搜索接口"></a>2.2.后台提供搜索接口</h2><h3 id="2-2-1-controller"><a href="#2-2-1-controller" class="headerlink" title="2.2.1.controller"></a>2.2.1.controller</h3><p>首先分析几个问题：</p>
<ul>
<li><p>请求方式：Post</p>
</li>
<li><p>请求路径：/search/page，不过前面的/search应该是网关的映射路径，因此真实映射路径page，代表分页查询</p>
</li>
<li><p>请求参数：json格式，目前只有一个属性：key-搜索关键字，但是搜索结果页一定是带有分页查询的，所以将来肯定会有page属性，因此我们可以用一个对象来接收请求的json数据：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SearchRequest</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String key;<span class="comment">// 搜索条件</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> Integer page;<span class="comment">// 当前页</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Integer DEFAULT_SIZE = <span class="number">20</span>;<span class="comment">// 每页大小，不从页面接收，而是固定大小</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Integer DEFAULT_PAGE = <span class="number">1</span>;<span class="comment">// 默认页</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getKey</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> key;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setKey</span><span class="params">(String key)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.key = key;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Integer <span class="title">getPage</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(page == <span class="keyword">null</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> DEFAULT_PAGE;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 获取页码时做一些校验，不能小于1</span></span><br><span class="line">        <span class="keyword">return</span> Math.max(DEFAULT_PAGE, page);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setPage</span><span class="params">(Integer page)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.page = page;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Integer <span class="title">getSize</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> DEFAULT_SIZE;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>返回结果：作为分页结果，一般都两个属性：当前页数据、总条数信息，我们可以使用之前定义的PageResult类</p>
</li>
</ul>
<p>代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@RequestMapping</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SearchController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> SearchService searchService;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 搜索商品</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> request</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span></span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="meta">@PostMapping(&quot;page&quot;)</span></span><br><span class="line">    <span class="keyword">public</span> ResponseEntity&lt;PageResult&lt;Goods&gt;&gt; search(<span class="meta">@RequestBody</span> SearchRequest request) &#123;</span><br><span class="line">        PageResult&lt;Goods&gt; result = <span class="keyword">this</span>.searchService.search(request);</span><br><span class="line">        <span class="keyword">if</span> (result == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">new</span> ResponseEntity&lt;&gt;(HttpStatus.NOT_FOUND);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> ResponseEntity.ok(result);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="2-2-2-service"><a href="#2-2-2-service" class="headerlink" title="2.2.2.service"></a>2.2.2.service</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">SearchService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> GoodsRepository goodsRepository;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> PageResult&lt;Goods&gt; <span class="title">search</span><span class="params">(SearchRequest request)</span> </span>&#123;</span><br><span class="line">        String key = request.getKey();</span><br><span class="line">        <span class="comment">// 判断是否有搜索条件，如果没有，直接返回null。不允许搜索全部商品</span></span><br><span class="line">        <span class="keyword">if</span> (StringUtils.isBlank(key)) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 构建查询条件</span></span><br><span class="line">        NativeSearchQueryBuilder queryBuilder = <span class="keyword">new</span> NativeSearchQueryBuilder();</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 1、对key进行全文检索查询</span></span><br><span class="line">        queryBuilder.withQuery(QueryBuilders.matchQuery(<span class="string">&quot;all&quot;</span>, key).operator(Operator.AND));</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 2、通过sourceFilter设置返回的结果字段,我们只需要id、skus、subTitle</span></span><br><span class="line">        queryBuilder.withSourceFilter(<span class="keyword">new</span> FetchSourceFilter(</span><br><span class="line">                <span class="keyword">new</span> String[]&#123;<span class="string">&quot;id&quot;</span>,<span class="string">&quot;skus&quot;</span>,<span class="string">&quot;subTitle&quot;</span>&#125;, <span class="keyword">null</span>));</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 3、分页</span></span><br><span class="line">        <span class="comment">// 准备分页参数</span></span><br><span class="line">        <span class="keyword">int</span> page = request.getPage();</span><br><span class="line">        <span class="keyword">int</span> size = request.getSize();</span><br><span class="line">        queryBuilder.withPageable(PageRequest.of(page - <span class="number">1</span>, size));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 4、查询，获取结果</span></span><br><span class="line">        Page&lt;Goods&gt; pageInfo = <span class="keyword">this</span>.goodsRepository.search(queryBuilder.build());</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 封装结果并返回</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> PageResult&lt;&gt;(goodsPage.getTotalElements(), goodsPage.getTotalPages(), goodsPage.getContent());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意点：我们要设置SourceFilter，来选择要返回的结果，否则返回一堆没用的数据，影响查询效率。</p>
<h3 id="2-2-3-测试"><a href="#2-2-3-测试" class="headerlink" title="2.2.3.测试"></a>2.2.3.测试</h3><p>刷新页面测试：</p>
<p><img src="assets/1532237344249.png" alt="1532237344249"></p>
<p><img src="assets/1532237401249.png" alt="1532237401249"></p>
<p>数据是查到了，但是因为我们只查询部分字段，所以结果json 数据中有很多null，这很不优雅。</p>
<p>解决办法很简单，在leyou-search的application.yml中添加一行配置，json处理时忽略空值：</p>
<figure class="highlight yaml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line">  <span class="attr">jackson:</span></span><br><span class="line">    <span class="attr">default-property-inclusion:</span> <span class="string">non_null</span> <span class="comment"># 配置json处理时忽略空值</span></span><br></pre></td></tr></table></figure>



<p>结果：</p>
<p><img src="assets/1532237986819.png" alt="1532237986819"></p>
<h2 id="2-3-页面渲染"><a href="#2-3-页面渲染" class="headerlink" title="2.3.页面渲染"></a>2.3.页面渲染</h2><p>页面已经拿到了结果，接下来就要渲染样式了。</p>
<h3 id="2-3-1-保存搜索结果"><a href="#2-3-1-保存搜索结果" class="headerlink" title="2.3.1.保存搜索结果"></a>2.3.1.保存搜索结果</h3><p>首先，在data中定义属性，保存搜索的结果：</p>
<p><img src="assets/1532239032197.png" alt="1532239032197"></p>
<p>在<code>loadData</code>的异步查询中，将结果赋值给<code>goodsList</code>：</p>
<p><img src="assets/1532239117076.png" alt="1532239117076"></p>
<h3 id="2-3-2-循环展示商品"><a href="#2-3-2-循环展示商品" class="headerlink" title="2.3.2.循环展示商品"></a>2.3.2.循环展示商品</h3><p>在search.html的中部，有一个<code>div</code>，用来展示所有搜索到的商品：</p>
<p><img src="assets/1532238893722.png" alt="1532238893722"></p>
<p>可以看到，<code>div</code>中有一个无序列表<code>ul</code>，内部的每一个<code>li</code>就是一个商品spu了。</p>
<p>我们删除多余的，只保留一个<code>li</code>，然后利用vue的循环来展示搜索到的结果：</p>
<p><img src="assets/1532239244410.png" alt="1532239244410"></p>
<h3 id="2-3-3-多sku展示"><a href="#2-3-3-多sku展示" class="headerlink" title="2.3.3.多sku展示"></a>2.3.3.多sku展示</h3><h4 id="2-3-3-1-分析"><a href="#2-3-3-1-分析" class="headerlink" title="2.3.3.1.分析"></a>2.3.3.1.分析</h4><p>接下来展示具体的商品信息，来看图：</p>
<p> <img src="assets/1526607712207.png" alt="1526607712207"></p>
<p>这里我们可以发现，一个商品位置，是多个sku的信息集合。<strong>当用户鼠标选择某个sku，对应的图片、价格、标题会随之改变！</strong></p>
<p>我们先来实现sku的选择，才能去展示不同sku的数据。</p>
<p> <img src="assets/1526654252710.png" alt="1526654252710"></p>
<p>可以看到，在列表中默认第一个是被选中的，那我们就需要做两件事情：</p>
<ul>
<li><p>在搜索到数据时，先默认把第一个sku作为被选中的，记录下来</p>
</li>
<li><p>记录当前被选中的是哪一个sku，记录在哪里比较合适呢？显然是遍历到的goods对象自己内部，因为每一个goods都会有自己的sku信息。</p>
</li>
</ul>
<h4 id="2-3-3-2-初始化sku"><a href="#2-3-3-2-初始化sku" class="headerlink" title="2.3.3.2.初始化sku"></a>2.3.3.2.初始化sku</h4><p>查询出的结果集skus是一个json类型的字符串，不是js对象</p>
<p><img src="assets/1532240220800.png" alt="1532240220800"></p>
<p>我们在查询成功的回调函数中，对goods进行遍历，把skus转化成对象，并添加一个selected属性保存被选中的sku：</p>
<p><img src="assets/1532240609206.png" alt="1532240609206"></p>
<p><img src="assets/1532240586769.png" alt="1532240586769"></p>
<h4 id="2-3-3-3-多sku图片列表"><a href="#2-3-3-3-多sku图片列表" class="headerlink" title="2.3.3.3.多sku图片列表"></a>2.3.3.3.多sku图片列表</h4><p>接下来，我们看看多个sku的图片列表位置：</p>
<p><img src="assets/1532240706261.png" alt="1532240706261"></p>
<p>看到又是一个无序列表，这里我们也一样删掉多余的，保留一个<code>li</code>，需要注意选中的项有一个样式类：selected</p>
<p>我们的代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--多sku图片列表--&gt;</span><br><span class="line">&lt;ul class&#x3D;&quot;skus&quot;&gt;</span><br><span class="line">    &lt;li :class&#x3D;&quot;&#123;selected: sku.id &#x3D;&#x3D; goods.selected.id&#125;&quot; v-for&#x3D;&quot;sku in goods.skus&quot; :key&#x3D;&quot;sku.id&quot;</span><br><span class="line">        @mouseEnter&#x3D;&quot;goods.selected&#x3D;sku&quot;&gt;</span><br><span class="line">        &lt;img :src&#x3D;&quot;sku.image&quot;&gt;</span><br><span class="line">    &lt;&#x2F;li&gt;</span><br><span class="line">&lt;&#x2F;ul&gt;</span><br></pre></td></tr></table></figure>

<p>注意：</p>
<ul>
<li>class样式通过 goods.selected的id是否与当前sku的id一致来判断</li>
<li>绑定了鼠标事件，鼠标进入后把当前sku赋值到goods.selected</li>
</ul>
<h3 id="2-3-4-展示sku其它属性"><a href="#2-3-4-展示sku其它属性" class="headerlink" title="2.3.4.展示sku其它属性"></a>2.3.4.展示sku其它属性</h3><p>现在，我们已经可以通过<code>goods.selected获取</code>用户选中的sku，那么我们就可以在页面展示了：</p>
<p><img src="assets/1526656197524.png" alt="1526656197524"></p>
<p>刷新页面：</p>
<p> <img src="assets/1526656243166.png" alt="1526656243166"></p>
<p>看起来很完美是吧！</p>
<p>但其实有一些瑕疵</p>
<h3 id="2-3-5-几个问题"><a href="#2-3-5-几个问题" class="headerlink" title="2.3.5.几个问题"></a>2.3.5.几个问题</h3><h4 id="2-3-5-1-价格显示的是分"><a href="#2-3-5-1-价格显示的是分" class="headerlink" title="2.3.5.1.价格显示的是分"></a>2.3.5.1.价格显示的是分</h4><p>首先价格显示就不正确，我们数据库中存放的是以分为单位，所以这里要格式化。</p>
<p>好在我们之前common.js中定义了工具类，可以帮我们转换。</p>
<p>改造：</p>
<p><img src="assets/1532242831006.png" alt="1532242831006"></p>
<p>结果报错：</p>
<p><img src="assets/1532242950035.png" alt="1532242950035"></p>
<p>为啥？</p>
<p>因为在Vue范围内使用任何变量，都会默认去Vue实例中寻找，我们使用ly，但是Vue实例中没有这个变量。所以解决办法就是把ly记录到Vue实例：</p>
<p><img src="assets/1532242983324.png" alt="1532242983324"></p>
<p>然后刷新页面：</p>
<p><img src="assets/1532243052100.png" alt="1532243052100"></p>
<h4 id="2-3-5-2-标题过长"><a href="#2-3-5-2-标题过长" class="headerlink" title="2.3.5.2.标题过长"></a>2.3.5.2.标题过长</h4><p>标题内容太长了，已经无法完全显示，怎么办？</p>
<p>截取一下：</p>
<p><img src="/assets/1526656959487.png" alt="1526656959487"></p>
<p>最好在加个悬停展示所有内容的效果</p>
<h4 id="2-3-5-3-sku点击不切换"><a href="#2-3-5-3-sku点击不切换" class="headerlink" title="2.3.5.3.sku点击不切换"></a>2.3.5.3.sku点击不切换</h4><p>还有一个错误比较隐蔽，不容易被发现。我们点击sku 的图片列表，发现没有任何变化。</p>
<p>这不科学啊，为什么？</p>
<p>通过控制台观察，发现数据其实是变化了，但是Vue却没有重新渲染视图。</p>
<p>这是因为Vue的自动渲染是基于对象的属性变化的。比如页面使用GoodsList进行渲染，如果GoodsList变化，或者其内部的任何子对象变化，都会Vue感知，从而从新渲染页面。</p>
<p>然而，这一切有一个前提，那就是当你第一次渲染时，对象中有哪些属性，Vue就只监视这些属性，后来添加的属性发生改变，是不会被监视到的。</p>
<p>而我们的goods对象中，本身是没有selected属性的，是我们后来才添加进去的：</p>
<p><img src="assets/1532243182104.png" alt="1532243182104"></p>
<p>这段代码稍微改造一下，即可：</p>
<p><img src="assets/1532243275078.png" alt="1532243275078"></p>
<p>也就是说，我们先把selected属性初始化完毕，然后才把整个对象赋值给goodsList，这样，goodsList已初始化时就有selected属性，以后就会被正常监控了。</p>
<p> <img src="assets/skus.gif"></p>
<h1 id="3-页面分页效果"><a href="#3-页面分页效果" class="headerlink" title="3.页面分页效果"></a>3.页面分页效果</h1><p>刚才的查询中，我们默认了查询的页码和每页大小，因此所有的分页功能都无法使用，接下来我们一起看看<code>分页功能条</code>该如何制作。</p>
<p>这里要分两步，</p>
<ul>
<li>第一步：如何生成分页条</li>
<li>第二步：点击分页按钮，我们做什么</li>
</ul>
<h2 id="3-1-如何生成分页条"><a href="#3-1-如何生成分页条" class="headerlink" title="3.1.如何生成分页条"></a>3.1.如何生成分页条</h2><p>先看下页面关于分页部分的代码：</p>
<p> <img src="H:assets/1526692249371.png" alt="1526692249371"></p>
<p>可以看到所有的分页栏内容都是写死的。</p>
<h3 id="3-1-1-需要的数据"><a href="#3-1-1-需要的数据" class="headerlink" title="3.1.1.需要的数据"></a>3.1.1.需要的数据</h3><p>分页数据应该是根据<strong>总页数</strong>、<strong>当前页</strong>、<strong>总条数</strong>等信息来计算得出。</p>
<ul>
<li>当前页：肯定是由页面来决定的，点击按钮会切换到对应的页</li>
<li>总页数：需要后台传递给我们</li>
<li>总条数：需要后台传递给我们</li>
</ul>
<p>我们首先在data中记录下这几个值：page-当前页，total-总条数，totalPage-总页数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">data: &#123;</span><br><span class="line">    ly,</span><br><span class="line">    search:&#123;</span><br><span class="line">        key: <span class="string">&quot;&quot;</span>,</span><br><span class="line">        page: <span class="number">1</span></span><br><span class="line">    &#125;,</span><br><span class="line">    goodsList:[], <span class="comment">// 接收搜索得到的结果</span></span><br><span class="line">    total: <span class="number">0</span>, <span class="comment">// 总条数</span></span><br><span class="line">    totalPage: <span class="number">0</span> <span class="comment">// 总页数</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>因为page是搜索条件之一，所以记录在search对象中。</p>
<p>要注意：我们在created钩子函数中，会读取url路径的参数，然后赋值给search。如果是第一次请求页面，page是不存在的。因此为了避免page被覆盖，我们应该这么做：</p>
<p><img src="assets/1532243978471.png" alt="1532243978471"></p>
<p>不过，这个时候我们自己的search对象中的值就可有可无了</p>
<h3 id="3-1-2-后台提供数据"><a href="#3-1-2-后台提供数据" class="headerlink" title="3.1.2.后台提供数据"></a>3.1.2.后台提供数据</h3><p>后台返回的结果中，要包含total和totalPage，我们改造下刚才的接口：</p>
<p>在我们返回的PageResult对象中，其实是有totalPage字段的：</p>
<p>  <img src="assets/1526695144476.png" alt="1526695144476"></p>
<p>我们在返回时，把这个值填上：</p>
<p> <img src="/assets/1526695592422.png" alt="1526695592422"></p>
<p>页面测试一下：</p>
<p><img src="assets/1532244453375.png" alt="1532244453375"></p>
<p>OK</p>
<h3 id="3-1-3-页面计算分页条"><a href="#3-1-3-页面计算分页条" class="headerlink" title="3.1.3.页面计算分页条"></a>3.1.3.页面计算分页条</h3><p>首先，把后台提供的数据保存在data中：</p>
<p> <img src="H:/%E4%B9%90%E4%BC%98/7%E6%9C%881%E5%8F%B7%E6%9B%B4%E6%96%B0/day12-%E5%9F%BA%E6%9C%AC%E6%90%9C%E7%B4%A2%E5%8A%9F%E8%83%BD/assets/1526695967230.png" alt="1526695967230"></p>
<p>然后看下我们要实现的效果：</p>
<p><img src="H:/%E4%B9%90%E4%BC%98/7%E6%9C%881%E5%8F%B7%E6%9B%B4%E6%96%B0/day12-%E5%9F%BA%E6%9C%AC%E6%90%9C%E7%B4%A2%E5%8A%9F%E8%83%BD/assets/1526695821870.png" alt="1526695821870"></p>
<p>这里最复杂的是中间的1~5的分页按钮，它需要动态变化。</p>
<p>思路分析：</p>
<ul>
<li>最多有5个按钮，因此我们可以用<code>v-for</code>循环从1到5即可</li>
<li>但是分页条不一定是从1开始：<ul>
<li>如果当前页值小于等于3的时候，分页条位置从1开始到5结束</li>
<li>如果总页数小于等于5的时候，分页条位置从1开始到5结束</li>
<li>如果当前页码大于3，应该从page-3开始</li>
<li>但是如果当前页码大于totalPage-3，应该从totalPage-5开始</li>
</ul>
</li>
</ul>
<p>所以，我们的页面这样来做：</p>
<p><img src="assets/1532246481241.png" alt="1532246481241"></p>
<p>a标签中的分页数字通过<code>index</code>函数来计算，需要把<code>i</code>传递过去：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">index(i)&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">this</span>.search.page &lt;= <span class="number">3</span> || <span class="built_in">this</span>.totalPage &lt;= <span class="number">5</span>)&#123;</span><br><span class="line">        <span class="comment">// 如果当前页小于等于3或者总页数小于等于5</span></span><br><span class="line">        <span class="keyword">return</span> i;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span>(<span class="built_in">this</span>.search.page &gt; <span class="number">3</span>) &#123;</span><br><span class="line">        <span class="comment">// 如果当前页大于3</span></span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.search.page - <span class="number">3</span> + i;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">this</span>.totalPage - <span class="number">5</span> + i;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>需要注意的是，如果总页数不足5页，我们就不应该遍历1<del>5，而是1</del>总页数，稍作改进：</p>
<p><img src="assets/1526698842013.png" alt="1526698842013"></p>
<p>分页条的其它部分就比较简单了：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&lt;div class&#x3D;&quot;sui-pagination pagination-large&quot;&gt;</span><br><span class="line">    &lt;ul style&#x3D;&quot;width: 550px&quot;&gt;</span><br><span class="line">        &lt;li :class&#x3D;&quot;&#123;prev:true,disabled:search.page &#x3D;&#x3D;&#x3D; 1&#125;&quot;&gt;</span><br><span class="line">            &lt;a href&#x3D;&quot;#&quot;&gt;«上一页&lt;&#x2F;a&gt;</span><br><span class="line">        &lt;&#x2F;li&gt;</span><br><span class="line">        &lt;li :class&#x3D;&quot;&#123;active: index(i) &#x3D;&#x3D;&#x3D; search.page&#125;&quot; v-for&#x3D;&quot;i in Math.min(5,totalPage)&quot; :key&#x3D;&quot;i&quot;&gt;</span><br><span class="line">            &lt;a href&#x3D;&quot;#&quot;&gt;&#123;&#123;index(i)&#125;&#125;&lt;&#x2F;a&gt;</span><br><span class="line">        &lt;&#x2F;li&gt;</span><br><span class="line">        &lt;li class&#x3D;&quot;dotted&quot; v-show&#x3D;&quot;totalPage &gt; 5&quot;&gt;&lt;span&gt;...&lt;&#x2F;span&gt;&lt;&#x2F;li&gt;</span><br><span class="line">        &lt;li :class&#x3D;&quot;&#123;next:true,disabled:search.page &#x3D;&#x3D;&#x3D; totalPage&#125;&quot;&gt;</span><br><span class="line">            &lt;a href&#x3D;&quot;#&quot;&gt;下一页»&lt;&#x2F;a&gt;</span><br><span class="line">        &lt;&#x2F;li&gt;</span><br><span class="line">    &lt;&#x2F;ul&gt;</span><br><span class="line">    &lt;div&gt;</span><br><span class="line">        &lt;span&gt;共&#123;&#123;totalPage&#125;&#125;页&amp;nbsp;&lt;&#x2F;span&gt;</span><br><span class="line">        &lt;span&gt;</span><br><span class="line">            到第</span><br><span class="line">            &lt;input type&#x3D;&quot;text&quot; class&#x3D;&quot;page-num&quot; :value&#x3D;&quot;search.page&quot;&gt;</span><br><span class="line">            页 &lt;button class&#x3D;&quot;page-confirm&quot; onclick&#x3D;&quot;alert(1)&quot;&gt;确定&lt;&#x2F;button&gt;</span><br><span class="line">        &lt;&#x2F;span&gt;</span><br><span class="line">    &lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;div&gt;</span><br></pre></td></tr></table></figure>





<h2 id="3-2-点击分页做什么"><a href="#3-2-点击分页做什么" class="headerlink" title="3.2.点击分页做什么"></a>3.2.点击分页做什么</h2><p>点击分页按钮后，自然是要修改<code>page</code>的值</p>
<p>所以，我们在<code>上一页</code>、<code>下一页</code>按钮添加点击事件，对page进行修改，在数字按钮上绑定点击事件，点击直接修改page：</p>
<p><img src="assets/1532248549662.png" alt="1532248549662"></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">prevPage()&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">this</span>.search.page &gt; <span class="number">1</span>)&#123;</span><br><span class="line">        <span class="built_in">this</span>.search.page--</span><br><span class="line">    &#125;</span><br><span class="line">&#125;,</span><br><span class="line">nextPage()&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="built_in">this</span>.search.page &lt; <span class="built_in">this</span>.totalPage)&#123;</span><br><span class="line">        <span class="built_in">this</span>.search.page++</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>当<code>page</code>发生变化，我们应该去后台重新查询数据。</p>
<p>不过，如果我们直接发起ajax请求，那么浏览器的地址栏中是不会有变化的，没有记录下分页信息。如果用户刷新页面，那么就会回到第一页。</p>
<p>这样不太友好，我们应该把<strong>搜索条件记录在地址栏的查询参数中</strong>。</p>
<p>因此，我们监听search的变化，然后把search的过滤字段拼接在url路径后：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">watch:&#123;</span><br><span class="line">    search:&#123;</span><br><span class="line">        deep:<span class="literal">true</span>,</span><br><span class="line">            handler(val)&#123;</span><br><span class="line">            <span class="comment">// 把search对象变成请求参数，拼接在url路径</span></span><br><span class="line">            <span class="built_in">window</span>.location.href = <span class="string">&quot;http://www.leyou.com/search.html?&quot;</span> + ly.stringify(val);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<p>刷新页面测试，然后就出现重大bug：页面无限刷新！为什么？</p>
<p>因为Vue实例初始化的钩子函数中，我们读取请求参数，赋值给search的时候，也触发了watch监视！也就是说，每次页面创建完成，都会触发watch，然后就会去修改window.location路径，然后页面被刷新，再次触发created钩子，又触发watch，周而复始，无限循环。</p>
<p>所以，我们需要在watch中进行监控，如果发现是第一次初始化，则不继续向下执行。</p>
<p>那么问题是，如何判断是不是第一次？</p>
<p>第一次初始化时，search中的key值肯定是空的，所以，我们这么做：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">watch:&#123;</span><br><span class="line">    search:&#123;</span><br><span class="line">        deep:<span class="literal">true</span>,</span><br><span class="line">            handler(val,old)&#123;</span><br><span class="line">            <span class="keyword">if</span>(!old || !old.key)&#123;</span><br><span class="line">                <span class="comment">// 如果旧的search值为空，或者search中的key为空，证明是第一次</span></span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 把search对象变成请求参数，拼接在url路径</span></span><br><span class="line">            <span class="built_in">window</span>.location.href = <span class="string">&quot;http://www.leyou.com/search.html?&quot;</span> + ly.stringify(val);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>再次刷新，OK了！</p>
<h2 id="3-3-页面顶部分页条"><a href="#3-3-页面顶部分页条" class="headerlink" title="3.3.页面顶部分页条"></a>3.3.页面顶部分页条</h2><p>在页面商品列表的顶部，也有一个分页条：</p>
<p> <img src="assets/1526716212704.png" alt="1526716212704"></p>
<p>我们把这一部分，也加上点击事件：</p>
<p><img src="assets/1532248435097.png" alt="1532248435097"></p>
<h1 id="4-排序-作业"><a href="#4-排序-作业" class="headerlink" title="4.排序(作业)"></a>4.排序(作业)</h1><h2 id="4-1-页面搜索排序条件"><a href="#4-1-页面搜索排序条件" class="headerlink" title="4.1.页面搜索排序条件"></a>4.1.页面搜索排序条件</h2><p>在搜索商品列表的顶部，有这么一部分内容：</p>
<p> <img src="assets/1526716565293.png" alt="1526716565293"></p>
<p>这是用来做排序的，默认按照综合排序。点击新品，应该按照商品创建时间排序，点击价格应该按照价格排序。因为我们没有统计销量和评价，这里咱们以<code>新品</code>和<code>价格</code>为例，进行讲解，做法是想通的。</p>
<p>排序需要知道两个内容：</p>
<ul>
<li>排序的字段</li>
<li>排序的方式</li>
</ul>
<p>因此，我们首先在<code>search</code>中记录这两个信息，因为created钩子函数会对search进行覆盖，因此我们在钩子函数中对这两个信息进行初始化即可：</p>
<p> <img src="assets/1526717586493.png" alt="1526717586493"></p>
<p>然后，在页面上给按钮绑定点击事件，修改<code>sortBy</code>和<code>descending</code>的值：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">&lt;!--排序字段--&gt;</span><br><span class="line">&lt;ul class&#x3D;&quot;sui-nav&quot;&gt;</span><br><span class="line">    &lt;li :class&#x3D;&quot;&#123;active:!search.sortBy&#125;&quot; @click&#x3D;&quot;search.sortBy&#x3D;&#39;&#39;&quot;&gt;</span><br><span class="line">        &lt;a href&#x3D;&quot;#&quot;&gt;综合&lt;&#x2F;a&gt;</span><br><span class="line">    &lt;&#x2F;li&gt;</span><br><span class="line">    &lt;li&gt;</span><br><span class="line">        &lt;a href&#x3D;&quot;#&quot;&gt;销量&lt;&#x2F;a&gt;</span><br><span class="line">    &lt;&#x2F;li&gt;</span><br><span class="line">    &lt;li @click&#x3D;&quot;search.sortBy&#x3D;&#39;createTime&#39;&quot; :class&#x3D;&quot;&#123;active: search.sortBy&#x3D;&#x3D;&#x3D;&#39;createTime&#39;&#125;&quot;&gt;</span><br><span class="line">        &lt;a href&#x3D;&quot;#&quot;&gt;新品&lt;&#x2F;a&gt;</span><br><span class="line">    &lt;&#x2F;li&gt;</span><br><span class="line">    &lt;li&gt;</span><br><span class="line">        &lt;a href&#x3D;&quot;#&quot;&gt;评价&lt;&#x2F;a&gt;</span><br><span class="line">    &lt;&#x2F;li&gt;</span><br><span class="line">    &lt;li @click&#x3D;&quot;search.sortBy&#x3D;&#39;price&#39;; search.descending &#x3D; !search.descending&quot;</span><br><span class="line">        :class&#x3D;&quot;&#123;active: search.sortBy&#x3D;&#x3D;&#x3D;&#39;price&#39;&#125;&quot;&gt;</span><br><span class="line">        &lt;a href&#x3D;&quot;#&quot;&gt;</span><br><span class="line">            价格</span><br><span class="line">            &lt;v-icon v-show&#x3D;&quot;search.descending&quot;&gt;arrow_drop_down&lt;&#x2F;v-icon&gt;</span><br><span class="line">            &lt;v-icon v-show&#x3D;&quot;!search.descending&quot;&gt;arrow_drop_up&lt;&#x2F;v-icon&gt;</span><br><span class="line">        &lt;&#x2F;a&gt;</span><br><span class="line">    &lt;&#x2F;li&gt;</span><br><span class="line">&lt;&#x2F;ul&gt;</span><br></pre></td></tr></table></figure>

<p>可以看到，页面请求参数中已经有了排序字段了：</p>
<p> <img src="assets/1526718252315.png" alt="1526718252315"></p>
<h2 id="4-2-后台添加排序逻辑"><a href="#4-2-后台添加排序逻辑" class="headerlink" title="4.2.后台添加排序逻辑"></a>4.2.后台添加排序逻辑</h2><p>接下来，后台需要接收请求参数中的排序信息，然后在搜索中加入排序的逻辑。</p>
<p>现在，我们的请求参数对象<code>SearchRequest</code>中，只有page、key两个字段。需要进行扩展：</p>
<p> <img src="assets/1526718448918.png" alt="1526718448918"></p>
<p>然后在搜索业务逻辑中，添加排序条件：</p>
<p><img src="assets/1526718637618.png" alt="1526718637618"></p>
<p>注意，因为我们存储在索引库中的的价格是一个数组，因此在按照价格排序时，会进行智能处理：</p>
<ul>
<li>如果是价格降序，则会把数组中的最大值拿来排序</li>
<li>如果是价格升序，则会把数组中的最小值拿来排序</li>
</ul>
<p><img src="assets/1526719415219.png" alt="1526719415219"></p>

            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        Author:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">AverageJoe</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        Link:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="http://www.averagejoe.top/2018/10/14/leyou/day12-bi-ji/day12-elasticsearch2/">http://www.averagejoe.top/2018/10/14/leyou/day12-bi-ji/day12-elasticsearch2/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        Reprint policy:
                    </i>
                </span>
                <span class="reprint-info">
                    All articles in this blog are used except for special statements
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    reprint polocy. If reproduced, please indicate source
                    <a href="/about" target="_blank">AverageJoe</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>Copied successfully, please follow the reprint policy of this article</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">more</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            <span class="chip bg-color">No tag</span>
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    
        <style>
    .valine-card {
        margin: 1.5rem auto;
    }

    .valine-card .card-content {
        padding: 20px 20px 5px 20px;
    }

    #vcomments textarea {
        box-sizing: border-box;
        background: url("/medias/comment_bg.png") 100% 100% no-repeat;
    }

    #vcomments p {
        margin: 2px 2px 10px;
        font-size: 1.05rem;
        line-height: 1.78rem;
    }

    #vcomments blockquote p {
        text-indent: 0.2rem;
    }

    #vcomments a {
        padding: 0 2px;
        color: #4cbf30;
        font-weight: 500;
        text-decoration: none;
    }

    #vcomments img {
        max-width: 100%;
        height: auto;
        cursor: pointer;
    }

    #vcomments ol li {
        list-style-type: decimal;
    }

    #vcomments ol,
    ul {
        display: block;
        padding-left: 2em;
        word-spacing: 0.05rem;
    }

    #vcomments ul li,
    ol li {
        display: list-item;
        line-height: 1.8rem;
        font-size: 1rem;
    }

    #vcomments ul li {
        list-style-type: disc;
    }

    #vcomments ul ul li {
        list-style-type: circle;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    #vcomments table, th, td {
        border: 0;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments h1 {
        font-size: 1.85rem;
        font-weight: bold;
        line-height: 2.2rem;
    }

    #vcomments h2 {
        font-size: 1.65rem;
        font-weight: bold;
        line-height: 1.9rem;
    }

    #vcomments h3 {
        font-size: 1.45rem;
        font-weight: bold;
        line-height: 1.7rem;
    }

    #vcomments h4 {
        font-size: 1.25rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    #vcomments h5 {
        font-size: 1.1rem;
        font-weight: bold;
        line-height: 1.4rem;
    }

    #vcomments h6 {
        font-size: 1rem;
        line-height: 1.3rem;
    }

    #vcomments p {
        font-size: 1rem;
        line-height: 1.5rem;
    }

    #vcomments hr {
        margin: 12px 0;
        border: 0;
        border-top: 1px solid #ccc;
    }

    #vcomments blockquote {
        margin: 15px 0;
        border-left: 5px solid #42b983;
        padding: 1rem 0.8rem 0.3rem 0.8rem;
        color: #666;
        background-color: rgba(66, 185, 131, .1);
    }

    #vcomments pre {
        font-family: monospace, monospace;
        padding: 1.2em;
        margin: .5em 0;
        background: #272822;
        overflow: auto;
        border-radius: 0.3em;
        tab-size: 4;
    }

    #vcomments code {
        font-family: monospace, monospace;
        padding: 1px 3px;
        font-size: 0.92rem;
        color: #e96900;
        background-color: #f8f8f8;
        border-radius: 2px;
    }

    #vcomments pre code {
        font-family: monospace, monospace;
        padding: 0;
        color: #e8eaf6;
        background-color: #272822;
    }

    #vcomments pre[class*="language-"] {
        padding: 1.2em;
        margin: .5em 0;
    }

    #vcomments code[class*="language-"],
    pre[class*="language-"] {
        color: #e8eaf6;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }

    #vcomments b,
    strong {
        font-weight: bold;
    }

    #vcomments dfn {
        font-style: italic;
    }

    #vcomments small {
        font-size: 85%;
    }

    #vcomments cite {
        font-style: normal;
    }

    #vcomments mark {
        background-color: #fcf8e3;
        padding: .2em;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }
</style>

<div class="card valine-card" data-aos="fade-up">
    <div class="comment_headling" style="font-size: 20px; font-weight: 700; position: relative; padding-left: 20px; top: 15px; padding-bottom: 5px;">
        <i class="fas fa-comments fa-fw" aria-hidden="true"></i>
        <span>评论</span>
    </div>
    <div id="vcomments" class="card-content" style="display: grid">
    </div>
</div>

<script src="/libs/valine/av-min.js"></script>
<script src="/libs/valine/Valine.min.js"></script>
<script>
    new Valine({
        el: '#vcomments',
        appId: 'BhOC9gMKAfPsRTjqr679tX7A-gzGzoHsz',
        appKey: 'xb9C1I9k62m6AJvin0UWGhSr',
        notify: 'false' === 'true',
        verify: 'false' === 'true',
        visitor: 'true' === 'true',
        avatar: 'mm',
        pageSize: '10',
        lang: 'en',
        placeholder: 'just go go'
    });
</script>

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;Previous</div>
            <div class="card">
                <a href="/2018/10/16/leyou/day13-bi-ji/day13-sou-suo-guo-lu/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/0.jpg" class="responsive-img" alt="">
                        
                        <span class="card-title"></span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            0.学习目标
了解过滤功能的基本思路
独立实现分类和品牌展示
了解规格参数展示
实现过滤条件筛选
实现已选过滤项回显
实现取消选择过滤项

1.过滤功能分析首先看下页面要实现的效果：

整个过滤部分有3块：

顶部的导航，已经选择的过滤条件
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2018-10-16
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-user fa-fw"></i>
                            AverageJoe
                            
                        </span>
                    </div>
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                Next&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2018/10/11/leyou/day11-bi-ji/day11-elasticsearch/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/0.jpg" class="responsive-img" alt="">
                        
                        <span class="card-title"></span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            0.学习目标
独立安装Elasticsearch
会使用Rest的API操作索引
会使用Rest的API查询数据
会使用Rest的API聚合数据
掌握Spring Data Elasticsearch使用

1.Elasticsearch介
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2018-10-11
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-user fa-fw"></i>
                            AverageJoe
                            
                        </span>
                    </div>
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


<!-- 代码块折行 -->

<style type="text/css">
code[class*="language-"], pre[class*="language-"] { white-space: pre !important; }
</style>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;TOC</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        display: none;
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        display: none;
        
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="503838841"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/libs/aplayer/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>

    
    <div class="container row center-align" style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            <span id="year">2019</span>
            <a href="/about" target="_blank">AverageJoe</a>
            |&nbsp;Powered by&nbsp;<a href="#" target="_blank">AverageJoe</a>
            |&nbsp;Theme&nbsp;<a href="#" target="_blank">Matery</a>
            <br>
            
            &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                class="white-color">213.1k</span>&nbsp;字
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">


    <a href="mailto:1533360044@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=1533360044" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 1533360044" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;Search</span>
            <input type="search" id="searchInput" name="s" placeholder="Please enter a search keyword"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script src="/js/search.js"></script>
<script type="text/javascript">
$(function () {
    searchFunc("/search.xml", 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    

    

    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    
	

	<div id="weather-v2-plugin-simple"></div>
<script>
WIDGET = {
  CONFIG: {
    "modules": "0124",
    "background": 1,
    "tmpColor": "FFFFFF",
    "tmpSize": 16,
    "cityColor": "FFFFFF",
    "citySize": 16,
    "aqiSize": 16,
    "weatherIconSize": 24,
    "alertIconSize": 18,
    "padding": "10px 10px 10px 10px",
    "shadow": "1",
    "language": "auto",
    "borderRadius": 5,
    "fixed": "false",
    "vertical": "middle",
    "horizontal": "center",
    "key": "XZgo8dbNqo"
  }
}
</script>
<script src="https://apip.weatherdt.com/simple/static/js/weather-simple-common.js?v=2.0"></script>
<script src="/js/cursor.js"></script>
</body>

</html>
